package com.meituan.controller;

import com.meituan.model.MerchantEntity;
import com.meituan.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class MerchantController {
    @Autowired
    MerchantService merchantService;

    @PostMapping("register")
    public Map<String,Object> register(@RequestBody  LinkedHashMap obj ){
        Map<String,Object> map =new HashMap<>();
        String username = (String) obj.get("username");
        String password = (String) obj.get("password");
        String phone = (String) obj.get("phone");
        MerchantEntity merchantEntity = new MerchantEntity();
        merchantEntity.setMerchantUsername(username);
        merchantEntity.setMerchantPassword(password);
        merchantEntity.setMerchantPhone(phone);
        if(merchantService.register(merchantEntity)!=0){
            map.put("err_code",0);
        }else{
            map.put("err_code",1);
        }

        return map;
    }

    @PostMapping("login")
    public Map<String,Object> login(@RequestBody LinkedHashMap obj){
        Map<String,Object> map = new HashMap<>();
        String merchantUsername = (String) obj.get("username");
        String merchantPassword = (String) obj.get("password");
        MerchantEntity result = merchantService.login(merchantUsername,merchantPassword);
        if(result!=null){
            map.put("err_code",0);
            map.put("result",result);
        }else{
            map.put("err_code",1);
        }
        return map;
    }


}
